package javacommon.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.util.StringTool;
 

/**
 * @author : csh
 * 
 */
public class ParamUtil {

	private static ParamUtil instance = new ParamUtil();

	private ParamUtil() {
	}

	public static ParamUtil getInstance() {
		if(null!=instance){
		   return instance;
		}else{
			return new ParamUtil();
		}
	}

	private static Map paramMap;
	public  static Map channelMap = new HashMap<String, Object>();
	
	public static Map changeObjectToMap(List<SysParam> paramList) {
		if (paramList != null && paramList.size() >= 0) {
			paramMap = new HashMap<String, String>();
			for (int i = 0, n = paramList.size(); i < n; i++) {
				SysParam sysParam = paramList.get(i);
				paramMap.put(sysParam.getMainKey(), sysParam.getParamValue());
				System.out.println("MainKey = "+sysParam.getMainKey()+", ParamValue= "+sysParam.getParamValue());
			}
			return paramMap;
		} else {
			return null;
		}
	}

	/**
	 * @param paramName
	 * @return
	 * @throws ReadParameterException
	 *             这个方法是提供给外界调用的方法;
	 */
	public static String getValueByName(String paramName) {
		if (paramMap != null && paramMap.containsKey(paramName)) {
			String value = (String) paramMap.get(paramName);

			return value.trim();

		} else {
			throw new NullPointerException("读取系统参数出现异常!!");
		}
	}
	
	/**
	 * @param paramName
	 * @return
	 * @throws ReadParameterException
	 *             这个方法是提供给外界调用的方法;
	 */
	public static void setChannelByPara(String paramkey,Object paramName) {
	
		if(paramkey!=null&&paramkey.length()>0){
			channelMap.put(paramkey, paramName);
		}
		
	}
  
	public static Object getChannelByPara(String paramName) {
		if (channelMap != null && channelMap.containsKey(paramName)) {
			Object value =  (Object) channelMap.get(paramName);

			return value;

		} else {
			return null;
			//throw new NullPointerException("读取通道参数出现异常!!");
		}
	}
	
	public static Object changenull(Object c){
		if(c==null){
			return false;
		}else{
			return  c;
		}
		
	}
	
}
